package com.lg.happyfarmer.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bigkoo.pickerview.TimePickerView;
import com.bigkoo.pickerview.listener.CustomListener;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.lg.happyfarmer.R;
import com.lg.happyfarmer.base.BaseActivity;
import com.lg.happyfarmer.base.BaseUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 生日选择
 * Created by Torres on 2018/3/30 0030.
 */

public class BirthdayActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.ib_back)
    ImageButton ib_back;
    @Bind(R.id.btn_test)
    Button btn_test;
    @Bind(R.id.tv_birth)
    TextView tv_birth;
    private TimePickerView warnTimer;//生日选择
    private String temp;
    @Bind(R.id.ll_all)
    LinearLayout ll_all;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_birthday);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
        BaseUtil.startLayoutAnim(mContext,ll_all);
        initEffectiveTimer();
        warnTimer.show();

    }
    /**
     * 显示年月日选择
     */
    private void initEffectiveTimer() {

        /**
         * @description
         *
         * 注意事项：
         * 1.自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针.
         * 具体可参考demo 里面的两个自定义layout布局。
         * 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
         * setRangDate方法控制起始终止时间(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
         */
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        Calendar startDate = Calendar.getInstance();
        startDate.set(1900, 1, 23);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2027, 2, 28);

        //时间选择器 ，自定义布局
        warnTimer = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                tv_birth.setText(getTime(date));
                btn_test.setEnabled(true);
            }


        })
                /*.setType(TimePickerView.Type.ALL)//default is all
                .setCancelText("Cancel")
                .setSubmitText("Sure")
                .setContentSize(18)
                .setTitleSize(20)
                .setTitleText("Title")
                .setTitleColor(Color.BLACK)
               /*.setDividerColor(Color.WHITE)//设置分割线的颜色
                .setTextColorCenter(Color.LTGRAY)//设置选中项的颜色
                .setLineSpacingMultiplier(1.6f)//设置两横线之间的间隔倍数
                .setTitleBgColor(Color.DKGRAY)//标题背景颜色 Night mode
                .setBgColor(Color.BLACK)//滚轮背景颜色 Night mode
                .setSubmitColor(Color.WHITE)
                .setCancelColor(Color.WHITE)*/
               /*.gravity(Gravity.RIGHT)// default is center*/
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setLayoutRes(R.layout.pickerview_custom_time, new CustomListener() {

                    @Override
                    public void customLayout(View v) {
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                        TextView tv_cancel = (TextView) v.findViewById(R.id.tv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                warnTimer.returnData();
                                warnTimer.dismiss();
                            }
                        });
                        tv_cancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                warnTimer.dismiss();
                            }
                        });
                    }
                })
                .setType(new boolean[]{true, true, true, false, false, false})
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(Color.RED)
                .build();


    }

    /**
     * 获取日期
     *
     * @param date
     * @return
     */
    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
        return format.format(date);
    }
    @Override
    protected void callBeforeDataBack(HemaNetTask netTask) {
        super.callBeforeDataBack(netTask);
    }

    @Override
    protected void callAfterDataBack(HemaNetTask netTask) {
        super.callAfterDataBack(netTask);
    }

    @Override
    protected void callBackForServerSuccess(HemaNetTask netTask, HemaBaseResult baseResult) {
        super.callBackForServerSuccess(netTask, baseResult);
    }

    @Override
    protected void callBackForServerFailed(HemaNetTask netTask, HemaBaseResult baseResult) {
        super.callBackForServerFailed(netTask, baseResult);
    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
        super.callBackForGetDataFailed(netTask, failedType);
    }

    @Override
    protected void findView() {

        super.findView();
    }

    @Override
    protected void getExras() {
        super.getExras();
    }

    @Override
    protected void setListener() {
        super.setListener();
        btn_test.setOnClickListener(this);
        ib_back.setOnClickListener(this);
        tv_birth.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent it=null;
        switch (view.getId()) {
            case R.id.tv_birth:
                warnTimer.show();
                break;
            case R.id.ib_back:
                finish();
                break;
            case R.id.btn_test:
                it=new Intent(mContext,MainActivity.class);
                startActivity(it);
                break;
        }
    }
}
